<?php


namespace App\Service;


use EchoPHP\Facades\Log;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use InvalidArgumentException;

class Crmapi {
    protected $baseUri;
    protected $errorMsg;

    public function __construct() {
        $this->baseUri = config('extraapi.crm.baseUri');
        if (!$this->baseUri) {
            throw new InvalidArgumentException('CRM接口地址未定义');
        }
    }

    /** 调用CRM客户信息接口
     * @param $crmcustomerId
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getCrmCustomerInfo($crmcustomerId) {
        $client = new Client([
            'base_uri' => $this->baseUri,
            'timeout' => 2
        ]);

        try {
            $response = $client->request('GET', 'GetAccountSubService.ashx', [
                'form_params' => ['AccSubID' => $crmcustomerId]
            ]);
            $content = $response->getBody()->getContents();
            $content = json_decode($content, true);
            if ($content['result'] == 0 || !isset($content['Item'][0])) {
                return [];
            }
            $contentItem = $content['Item'][0];
            unset($contentItem['AccSubName']);
            $contentItem['SubName'] = $content['Item'][0]['AccSubName'];
            $contentItem['Industry'] = empty($contentItem['Industry']) ? '' : $contentItem['Industry'];
            $contentItem = array_change_key_case($contentItem, CASE_LOWER);
            return $contentItem;
        } catch (RequestException $e) {
            if ($e->hasResponse()) {
                $this->errorMsg = $e->getResponse()->getBody()->getContents();
            } else {
                $this->errorMsg = $e->getMessage();
            }
            $this->errorMsg .= ';' . $e->getFile() . ':[line:' . $e->getLine() . ']';
            Log::error($this->errorMsg);
            return [];
        } catch (Exception $e) {
            $this->errorMsg = $e->getMessage() . ';' . $e->getFile() . ':[line:' . $e->getLine() . ']';
            Log::error($this->errorMsg);
            return [];
        }
    }

    /**调用CRM项目信息接口
     * @param $subid
     * @param $crmNum
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getCrmProjectInfo($crmNum) {
        $client = new Client([
            'base_uri' => $this->baseUri,
            'timeout' => 2
        ]);
        try {
            $response = $client->request('GET', 'GetBigProjectByNum.ashx', [
                'form_params' => ['proBigNumber' => $crmNum]
            ]);
            $content = $response->getBody()->getContents();
            $content = json_decode($content, true);
            $content = $content[0];
            if ($content['result'] == 0){
                return [];
            }
            return array_change_key_case($content['Item'][0]);
        } catch (RequestException $e) {
            if ($e->hasResponse()) {
                $this->errorMsg = $e->getResponse()->getBody()->getContents();
            } else {
                $this->errorMsg = $e->getMessage();
            }
            $this->errorMsg .= ';' . $e->getFile() . ':[line:' . $e->getLine() . ']';
            Log::error($this->errorMsg);
            return [];
        } catch (Exception $e) {
            $this->errorMsg = $e->getMessage() . ';' . $e->getFile() . ':[line:' . $e->getLine() . ']';
            Log::error($this->errorMsg);
            return [];
        }
    }

    public function getCrmProjectList($subId) {
        $client = new Client([
            'base_uri' => $this->baseUri,
            'timeout' => 2
        ]);
        $bigProjectInfo = [];
        try {
            $response = $client->request('GET', 'GetProBigService.ashx', [
                'form_params' => ['accsubID' => $subId]
            ]);
            $content = $response->getBody()->getContents();
            $content = json_decode($content, true);
            if ($content['result'] == 0){
                return [];
            }
            foreach ($content['Item'] as $item) {
                if ($item['ProStatus'] == '01'){
                    $bigProjectInfo []= array_change_key_case($item);
                }
            }
            return $bigProjectInfo;
        } catch (RequestException $e) {
            if ($e->hasResponse()) {
                $this->errorMsg = $e->getResponse()->getBody()->getContents();
            } else {
                $this->errorMsg = $e->getMessage();
            }
            $this->errorMsg .= ';' . $e->getFile() . ':[line:' . $e->getLine() . ']';
            Log::error($this->errorMsg);
            return $bigProjectInfo;
        } catch (Exception $e) {
            $this->errorMsg = $e->getMessage() . ';' . $e->getFile() . ':[line:' . $e->getLine() . ']';
            Log::error($this->errorMsg);
            return $bigProjectInfo;
        }
    }
}
